// xc_lshop/index/com/jtk.js
// var smapidata = require('../model/smapi_data.js')
var smapidata = require('../../../xc/model/smapi_data')

// var smapidata = require('../../../xc_lshop/savemoney/model/smapi_data.js')

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    config: {
      type: Object,
      value: {},
      observer: function (newVal, oldVal) {
        if (newVal) {
          this.init()
        }

        // 属性值变化时执行
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init() {
      console.log("this.data.config");
      console.log(this.data.config);
      var config = this.data.config;
      var update = {};
      if (config.jtklmapi && config.jtklmapi.status == 1) {
        if (config.jtklmapi.showpro && config.jtklmapi.showpro == 1) {
          console.log("dddddddddddd");

          update['show'] = true
          update['smapiconfig'] = config.jtklmapi.home
          update['strollconfig'] = config.jtklmapi.stroll
          this.setData(update)
          this.getnavdata()
        }
      }

    },
    getnavdata() {
      var that = this;
      smapidata.getnavdata().then(function (ret) {
        console.log("ret")
        console.log(ret)
        if (ret['status'] = 1) {
          var update = {
            platform: ret['data']
          }
          if (ret['data'][0]) {
            update['source'] = ret['data'][0]['source']
          }
          that.setData(update)
          that.getclass(that.data.source)


        } else {
          var message = ret['message'] ? ret['message'] : "获取失败";
          that.error(message, false)
        }

      })
    },
    getclass(source) {
      var that = this;
      this.setData({
        class: []
      })
      smapidata.getclass(source).then(function (ret) {
        // console.log("getclass")
        // console.log(ret)
        if (ret['status'] = 1) {
          console.log('getclass')

          console.log(ret['data'])
          var update = {
            class: ret['data']
          }
          if (ret['data']['1']) {
            update['classid'] = '1'
          }
          that.setData(update)
          that.getpagelist(that, 1)

        } else {
          var message = ret['message'] ? ret['message'] : "获取失败";
          that.error(message, false)
        }

      })
    },
    getpagelist(that, rest, strwhere) {
      if (rest == 1) {
        if (!strwhere) {
          strwhere = {};
          strwhere['source'] = this.data.source
          strwhere['classid'] = this.data.classid

          // strwhere = this.data.searchdata
        }
        that.setData({
          testloading: true,
        });
      }
      smapidata.offset(that, rest, strwhere).then(function (result) {
        var updatemdoel = {};
        if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {

          if (rest == 1) {
            updatemdoel["modellist"] = result["obj"]["modellist"]['data'];
          } else {
            if (result["obj"]["modellist"]['data'].length > 0) {
              updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]['data']);
            }
          }
        }
        // that.selectComponent("#ordercomment").showmodel( updatemdoel["modellist"][0],"order")      
        // wx.stopPullDownRefresh();
        //  that.setData({
        //   loading:true,
        //   modellist: []
        // });

        updatemdoel['testloading'] = false;
        console.log("updatemdoel")
        console.log(updatemdoel)
        that.setData(updatemdoel);
      }, function () {

      });
    },
    onReachBottom: function () {
      var that = this;
      that.getpagelist(that, -1)
    },
    onChange(e) {
      var that = this;
      this.setData({
        classid: e.detail.name
      })
      that.getpagelist(that, 1)
    },
    changesource(e) {
      console.log(e)
      var dataset = e.currentTarget.dataset;
      this.setData({
        source: dataset.source
      })
      this.getclass(dataset.source)

    },
  }
})